﻿using AutoMapper;
using eCommerceApp.Application.DTOS;
using eCommerceApp.Application.DTOS.Product;
using eCommerceApp.Application.Services.Interfaces;
using eCommerceApp.Domain.Entities;
using eCommerceApp.Domain.Interfaces;

namespace eCommerceApp.Application.Services.Implementations
{
    public class ProductService(IGeneric<Product> productInteface,IMapper mapper) : IProductService
    {
        public async Task<IEnumerable<GetProduct>> GetAllAsync()
        {
            var rawData = await productInteface.GetAllAsync();
            if (!rawData.Any()) return [];

            return mapper.Map<IEnumerable<GetProduct>>(rawData);
            
        }

        public async Task<GetProduct> GetByIdAsync(Guid id)
        {
            var rawData = await productInteface.GetByIdAsync(id);
            if (rawData == null) return new GetProduct();

            return mapper.Map<GetProduct>(rawData);
        }

        public async Task<ServiceResponse> AddAsync(CreateProduct product)
        {
            var mappedData = mapper.Map<Product>(product);
            int result = await productInteface.AddAsync(mappedData);
           
            return result > 0 ?
               new ServiceResponse(true, "Product added!"):
               new ServiceResponse(false, "Product failed to be added!");
        }

        public async Task<ServiceResponse> UpdateAsync(UpdateProduct product)
        {
            var mappedData = mapper.Map<Product>(product);
            int result = await productInteface.UpdateAsync(mappedData);
            return result > 0 ?
               new ServiceResponse(true, "Product update!") :
               new ServiceResponse(false, "Product failed to be update!");
        }

        public async Task<ServiceResponse> DeleteAsync(Guid id)
        {
            int result = await productInteface.DeleteAsync(id);
           
            if (result > 0)
                return new ServiceResponse(true, "Product deleted");
            return new ServiceResponse(false, "Product failed to be deleted");
        }
    }
}
